class MachinesController < ApplicationController
  # GET /machines
  # GET /machines.xml
  def index
   
    if !params[:salleid].nil?
      
      @machines = Machine.find_by_sql("SELECT machines.*, salles.nomsalle FROM machines INNER JOIN salles ON machines.salleid = salles.id
                                   and machines.salleid ='#{params[:salleid]}'  order by machines.id");
    else

      @machines = Machine.find_by_sql("SELECT machines.*, salles.nomsalle FROM machines INNER JOIN salles ON machines.salleid = salles.id order by machines.id")
    end
     ##p "test",@machines
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @machines }
    end
  end
  
  def machinebysalle
     @machines = Machine.find_by_sql("SELECT machines.*, salles.nomsalle FROM machines INNER JOIN salles ON machines.salleid = salles.id
                                   and machines.salleid ='#{params[:salleid]}'  order by machines.id");
     respond_to do |format|
        format.html # index.html.erb
        format.xml  { render :xml => @machines }
     end
  end

  # GET /machines/1
  # GET /machines/1.xml
  def show
    @machine = Machine.find(params[:id])
   #@machine = Salle.find(@machine.id)
    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @machine }
    end
  end

  # GET /machines/new
  # GET /machines/new.xml
  def new
    @machine = Machine.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @machine }
    end
  end

  # GET /machines/1/edit
  def edit
    @machine = Machine.find(params[:id])
 
  end

  # POST /machines
  # POST /machines.xml
  def create
    @machine = Machine.new(params[:machine])

    respond_to do |format|
      #p 'test',  params[:machine][:nommachine]
      if @machine.save
            flash[:notice] = 'Machine créée avec succes.'
            format.html { redirect_to(@machine) }
            format.xml  { render :xml => @machine, :status => :created, :location => @machine }
      else
        format.html { render :action => "new" }
         flash[:error] = 'Veillez remplir correctement les champs.'
       # flash[:error] = 'Machine créée avec succes.'
        format.xml  { render :xml => @machine.errors, :status => :unprocessable_entity }
      end
     
    end
  end

  # PUT /machines/1
  # PUT /machines/1.xml
  def update
    @machine = Machine.find(params[:id])

    respond_to do |format|
      if @machine.update_attributes(params[:machine])
        flash[:notice] = 'Machine mise à jour.'
        format.html { redirect_to(@machine) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @machine.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /machines/1
  # DELETE /machines/1.xml
  def destroy
    @machine = Machine.find(params[:id])
    @machine.destroy

    respond_to do |format|
      format.html { redirect_to(machines_url) }
      format.xml  { head :ok }
    end
  end

  
  #
  #liste de salle
  def listesalle
    @salles = Salle.all
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @salles }
    end
  end
  def getsallebyid
    @salles = Salle.find(params[:id])
    respond_to do |format|
      format.html # index.html.erb
      format.xml{ render :xml => @salles}
    end
  end
  def deplacermachine
    @machine = Machine.find(params[:id])
      respond_to do |format|
      format.html # deplacermachine.html.erb
      format.xml  { render :xml => @machine }
      end
  end

   def rechercher
       @machines = Machine.find_by_sql("Select machines.*, salles.nomsalle FROM machines INNER JOIN salles ON machines.salleid = salles.id
             where nommachine like '%#{params[:name]}%' order by machines.id ")
      
      respond_to do |format|
          format.html # rechercher.html.erb
          format.xml  { render :xml => @machines }
      end unless (@machines.id)
   end

   def addmachinetosalle
      @car = Machine.new(params[:salleid])
      @machine = Machine.new
      respond_to do |format|
          format.html # new.html.erb
          format.xml  { render :xml => @machine }
      end
   end

   def interomachine
     if params[:tab_incidents]
       @tab_inci = params[:tab_incidents]
     end
     @machine = Machine.find(params[:id])
     @regles = Regle.find(:all)

      respond_to do |format|
          format.html # new.html.erb
          format.xml  { render :xml => @machine }
      end
   end
end
